package com.bootdo.system.controller;

import com.bootdo.common.utils.ExcelFormatUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.domain.ReportDo;
import com.bootdo.system.domain.ScanIpRangeDO;
import com.bootdo.system.domain.ScanequipDO;
import com.bootdo.system.service.ScanIpRangeService;
import com.bootdo.system.service.ScanequipService;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-12-22 16:06:00
 */
@Controller
@RequestMapping("/system/scanequip")
public class ScanequipController {

	@Autowired
	private ScanequipService scanequipService;

	@Autowired
	private ScanIpRangeService scanIpRangeService;


	/**
	 * ip可视化页面
	 * @return
	 */
	@GetMapping("/visualization")
	String visualization(){
	    return "system/scanequip/visualization";
	}

	/**
	 * 设备列表
	 * @return
	 */
	@GetMapping("/equipment")
	String equipment(){
		return "system/scanequip/equipment";
	}

	/**
	 * 获取ip可视化页面数据
	 * @param
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getVisualizationList")
	public Map<String,Object> map(ScanequipDO scanequipDO){
		//查询列表数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rangeid",scanequipDO.getRangeid());
		List<ScanequipDO> scanequipList = scanequipService.getVisualizationList(map);
		ScanIpRangeDO scanIpRange = scanIpRangeService.get(scanequipDO.getRangeid());
		String ipStart = scanIpRange.getIpStart();
		String ipEnd = scanIpRange.getIpEnd();
		int prefix1 = ipStart.lastIndexOf(".");
		//获取网段范围
		int prefix4 = Integer.parseInt(ipStart.substring(prefix1 + 1,ipStart.length()));
		int prefix5 = Integer.parseInt(ipEnd.substring(prefix1 + 1,ipEnd.length()));
		map.put("startIp",prefix4);
		map.put("endIp",prefix5);
		map.put("scanequipList",scanequipList);
		return map;
	}

	/**
	 * 获取设备列表数据
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/equipmentList")
	public PageUtils equipmentList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<ScanequipDO> scanequipList = scanequipService.equipmentList(query);
		int total = scanequipService.countEquipment(query);
		PageUtils pageUtils = new PageUtils(scanequipList, total);
		return pageUtils;
	}

	/**
	 * 导出设备列表数据
	 * @param scanequipDO
	 */
	@ResponseBody
	@GetMapping("/exportEquipmentList")
	public ResponseEntity<byte[]> exportEquipmentList(ScanequipDO scanequipDO){
		try {
			Map<String, Object> params= new HashMap<>();
			params.put("ipC",scanequipDO.getIpC());
			params.put("macC",scanequipDO.getMacC());
			params.put("nflag1",scanequipDO.getNflag1());
			params.put("ostype",scanequipDO.getOstype());
			List<ScanequipDO> scanequipList = scanequipService.equipmentList(params);
			return ExcelFormatUtil.buildResponseEntity(reportExcel(scanequipList), "网络设备报表.xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 数据导出
	 * @param scanequipList
	 * @return
	 */
	private InputStream reportExcel(List<ScanequipDO> scanequipList) {
		ByteArrayOutputStream output = null;
		InputStream inputStream = null;
		SXSSFWorkbook wb = new SXSSFWorkbook(1000);
		SXSSFSheet sheet = wb.createSheet();
		// 设置报表头样式
		CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
		CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

		String[] str = new String[] {"IP地址", "mac地址","设备系统", "设备类型","生成日期", "更改日期","是否在线", "部门"};

		// 字段名所在表格的宽度
		int[] ints = new int[] {5000, 5000,5000, 5000,5000, 5000,5000, 5000,};

		// 设置表头样式
		ExcelFormatUtil.initTitleEX(sheet, header, str, ints);

		if (!CollectionUtils.isEmpty(scanequipList)) {
			int i= 0;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for (ScanequipDO data : scanequipList) {
				i++;
				SXSSFRow row = sheet.createRow(i);
				int j = 0;
				SXSSFCell cell = row.createCell(j++);
				cell.setCellValue(data.getIpC());
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(data.getMacC());
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(data.getMatchos());
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(data.getOstype());
				cell.setCellStyle(content);

				String firsttimeValue = "";
				if(null != data.getFirsttime()) {
					firsttimeValue = sdf.format(data.getFirsttime());
				}
				cell = row.createCell(j++);
				cell.setCellValue(firsttimeValue);
				cell.setCellStyle(content);

				String lasttimeValue = "";
				if(null != data.getLasttime()) {
					lasttimeValue = sdf.format(data.getLasttime());
				}
				cell = row.createCell(j++);
				cell.setCellValue(lasttimeValue);
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				String nameValue = "";
				if(null != data.getNflag1() && data.getNflag1() == 1) {
					nameValue = "在线";
				}else if(null != data.getNflag1() && data.getNflag1() == 0) {
					nameValue = "离线";
				}
				cell.setCellValue(nameValue);
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(data.getGroupname());
				cell.setCellStyle(content);
			}
		}
		try {
			output = new ByteArrayOutputStream();
			wb.write(output);
			inputStream = new ByteArrayInputStream(output.toByteArray());
			output.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (output != null) {
					output.close();
					if (inputStream != null){
						inputStream.close();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return inputStream;
	}
	
}
